package views;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.view.SurfaceHolder.Callback;

import com.tetris.project.Shr_Res;

import engine.GameThread;
import engine.PrintingThread;

public class GamePlayView extends SurfaceView implements Callback {

	public GameThread gameThread;
	public PrintingThread boardStatus;
	int x = 0;
	int y = 0;
	public static int mWindowWidth;// = 320;//HVGA
	public static int mWindowHeight;// = 480;//HVGA
	public static WindowManager wM;

	public GamePlayView(Context context, AttributeSet attrs) {
		super(context, attrs);
		SurfaceHolder holder = getHolder();
		holder.addCallback(this);
		setFocusable(true);
		wM = (WindowManager)(context.getSystemService(Context.WINDOW_SERVICE));
		Display display = wM.getDefaultDisplay();
		mWindowWidth  = display.getWidth();
		mWindowHeight = display.getHeight();
		gameThread = new GameThread(holder, context, this);
		boardStatus = new PrintingThread(gameThread.mScene.mBoard);
	}

	public GamePlayView(Context context) {
		super(context);
		SurfaceHolder holder = getHolder();
		holder.addCallback(this);
		setFocusable(true);
		gameThread = new GameThread(holder, context, this);
		boardStatus = new PrintingThread(gameThread.mScene.mBoard);
	}
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		// TODO Auto-generated method stub
		mWindowWidth = width;
		mWindowHeight = height;
		gameThread.mScene.mBoardUI.setWindowDimensions(width, height);
		Log.d(Shr_Res.TAG, "SurfaceChanged() mWindowHeight = "+mWindowHeight);
		Log.d(Shr_Res.TAG, "SurfaceChanged() mWindowWidth = "+mWindowWidth);
		gameThread.mScene.mBoard.defineBoardSize();
		gameThread.mScene.mCurrentPiece.resetImageCoordinates();
		gameThread.setRunning(true);
		Shr_Res.printing = true;
		gameThread.start();
		boardStatus.start();
		Shr_Res.level = engine.BoardUI.counter = 0;
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		gameThread.setRunning(false);
		Shr_Res.printing = false;
		Log.d(Shr_Res.TAG, "The Surface was created");
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		boolean retry = true;
		gameThread.setRunning(false);

		Log.d(Shr_Res.TAG, "Destroying the Surface created...");
		while (retry) {
			try {
				Log.d(Shr_Res.TAG, "Joining Game Thread...");
				gameThread.join();
				retry = false;
			} catch (InterruptedException e) {
			}
		}

		Shr_Res.printing = false;
		retry = true;
		while (retry) {
			try {
				Log.d(Shr_Res.TAG, "I'll wake up PrintingThread");
				Log.d(Shr_Res.TAG, "Printing Thread Joined...");
				boardStatus.join();
				retry = false;
			} catch (InterruptedException e) {
			}
		}
	}
}
